home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / GRAPHICS / VOXRAY.ZIP / PLAYER.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-25  |  11.9 KB  |  380 lines

  1. #include "ray.h"
  2. #include "globals.h"
  3. #include "rayspr.h"
  4. #include "rayfile.h"
  5. #include "sprfunc.h"
  6. #include "sfvars.h"
  7. #include "defobj.h"
  8. #include "playint.h"
  9. #include "keyinfo.h"
  10. #include "sprtypes.h"
  11. #include "voxinter.h"
  12. #include "message.h"
  13. #include "classes.h"
  14. #include "quit.h"
  15. #include "inventor.h"
  16. #include "tags.h"
  17. #include "shot.h"
  18.  
  19. #define GUN_HEIGHT cur_obj->type->eye_height-20
  20. #define MAX_TIME_TO_GUN 5
  21. #define MAX_FALL_PER_UPDATE 8
  22. #define NO_INPUT 0
  23.  
  24. short default_input[MAX_TRACKED_INPUTS]={0,0,0,0,0,0,0};
  25. PSHORT cur_input_buffer=default_input;
  26.  
  27. typedef struct PLAYER_INFO * pplayer_info;
  28. typedef struct PLAYER_INFO {
  29.    PSHORT input_buffer;
  30.    SHORT tracked_inputs[MAX_TRACKED_INPUTS];
  31.    BOOL fly_mode;
  32.    MYFIXED base_fixed_z, cur_fixed_z;
  33.    angle_type z_move_angle;
  34.    SHORT dis_to_fall;
  35.    LONG time_to_gun;
  36.    angle_type vert_angle;
  37.    pobject_node inventory;
  38. } player_info;
  39.  
  40. void Setup_Player_Inputs(pplayer_info cur_extra);
  41. void Player_Update(pobject cur_obj, long update_num);
  42. void Player_Update_Z(pobject cur_obj, psector new_sec);
  43. void Player_Load(pobject cur_obj, long offset);
  44. void Z_Correct_Move(pobject cur_obj);
  45. void Reset_Player_Z(pplayer_info cur_extra);
  46. pobject Scan_Inventory(pobject_node inventory, ULONG search_class);
  47. ULONG Player_Message(pobject send_obj, pobject receive_obj, ULONG message, pdata extra_data);
  48.  
  49. void Init_Player_Obj(func_index index)
  50. {
  51. update_funcs[index]=Player_Update;
  52. update_z_funcs[index]=Player_Update_Z;
  53. load_extra_funcs[index]=Player_Load;
  54. message_funcs[index]=Player_Message;
  55. }
  56.  
  57. void Player_Load(pobject cur_obj, long offset)
  58. {
  59.  
  60.    pplayer_info new_info;
  61.    new_info=(pplayer_info)NewPtr(sizeof(player_info));
  62.  
  63.    for (short cur_input=0; cur_input < MAX_TRACKED_INPUTS; cur_input++) {
  64.       new_info->tracked_inputs[cur_input]=0;
  65.    }
  66.    new_info->fly_mode=FALSE;
  67.    new_info->base_fixed_z=cur_obj->z << SHIFT;
  68.    new_info->cur_fixed_z=new_info->base_fixed_z;
  69.    new_info->z_move_angle=ANGLE_0;
  70.    new_info->dis_to_fall=0;
  71.    new_info->input_buffer=cur_input_buffer;
  72.    new_info->vert_angle=ANGLE_0;
  73.    new_info->inventory=NULL;
  74.    cur_obj->extra_data=new_info;
  75.  
  76. }
  77.  
  78. void Player_Update_Z(pobject cur_obj, psector new_sec)
  79. {
  80.    pplayer_info cur_extra=(pplayer_info)cur_obj->extra_data;
  81.    short height_dif;
  82.  
  83.    if (!(new_sec->flags & VOXEL_SECTOR)) {
  84.       height_dif=cur_obj->cur_sec->floor_height-new_sec->floor_height;
  85.    } else {
  86.       height_dif=cur_obj->cur_sec->floor_height-
  87.         Get_Voxel_Alt((PUCHAR)new_sec->extra_data, cur_obj->x, cur_obj->y);
  88.    }
  89.  
  90.    // if in fly mode, changing sectors has no effect on overall z, so we must alter
  91.    // z for difference in floor height
  92.    if (cur_extra->fly_mode) {
  93.       cur_obj->z+=height_dif;
  94.       cur_extra->base_fixed_z=cur_obj->z<<SHIFT;
  95.       cur_extra->cur_fixed_z=cur_extra->base_fixed_z;
  96.    } else {
  97.       if (height_dif>0) {
  98.          cur_obj->z+=height_dif;
  99.          cur_extra->base_fixed_z+=(height_dif<<SHIFT);
  100.          cur_extra->cur_fixed_z+=(height_dif<<SHIFT);
  101.          cur_extra->dis_to_fall+=height_dif;
  102.       } else {
  103.          if (cur_extra->dis_to_fall>0) {
  104.            cur_obj->z-=cur_extra->dis_to_fall;
  105.            cur_extra->base_fixed_z-=(cur_extra->dis_to_fall << SHIFT);
  106.            cur_extra->cur_fixed_z-=(cur_extra->dis_to_fall << SHIFT);
  107.            cur_extra->dis_to_fall=0;
  108.          }
  109.       }
  110.    } /* endif */
  111. }
  112.  
  113. void Player_Update(pobject cur_obj, long update_num) {
  114.      MYFIXED dx, dy;
  115.      vector2 delta_vec;
  116.      SHORT amount_up, amount_down, amount_left, amount_right, amount_look_up, 
  117.         amount_look_down, raw_input, gun_shot;
  118.  
  119.      pplayer_info cur_extra=(pplayer_info)cur_obj->extra_data;
  120.      Setup_Player_Inputs(cur_extra);     
  121.      amount_up=cur_extra->tracked_inputs[INDEX_UP];
  122.      amount_down=cur_extra->tracked_inputs[INDEX_DOWN];
  123.      amount_left=cur_extra->tracked_inputs[INDEX_LEFT];
  124.      amount_right=cur_extra->tracked_inputs[INDEX_RIGHT];
  125.      amount_look_up=cur_extra->tracked_inputs[LOOK_UP];
  126.      amount_look_down=cur_extra->tracked_inputs[LOOK_DOWN];
  127.      gun_shot=cur_extra->tracked_inputs[INDEX_GUN];
  128.      raw_input=cur_extra->tracked_inputs[RAW_INPUT];
  129.  
  130.      cur_extra->time_to_gun--;
  131.      // do gravity for sector change
  132.      if (cur_extra->dis_to_fall>0) {
  133.         if (cur_extra->dis_to_fall>MAX_FALL_PER_UPDATE) {
  134.            cur_extra->dis_to_fall-=MAX_FALL_PER_UPDATE;
  135.            cur_obj->z-=MAX_FALL_PER_UPDATE;
  136.            cur_extra->base_fixed_z-=(MAX_FALL_PER_UPDATE << SHIFT);
  137.            cur_extra->cur_fixed_z-=(MAX_FALL_PER_UPDATE << SHIFT);
  138.         } else {
  139.            cur_obj->z-=cur_extra->dis_to_fall;
  140.            cur_extra->base_fixed_z-=(cur_extra->dis_to_fall << SHIFT);
  141.            cur_extra->cur_fixed_z-=(cur_extra->dis_to_fall << SHIFT);
  142.            cur_extra->dis_to_fall=0;
  143.         }
  144.      }
  145.  
  146.         // reset deltas
  147.  
  148.         dx=dy=0;
  149.  
  150.         // what is user doing
  151.  
  152.         if (amount_right)
  153.            {
  154.            // rotate player right
  155.  
  156.            cur_obj->angle=Get_Angle_Difference(cur_obj->angle, amount_right);
  157.  
  158.            } // end if right
  159.         else
  160.         if (amount_left)
  161.            {
  162.            // rotate player to left
  163.  
  164.            cur_obj->angle=Get_Angle_Sum(cur_obj->angle, amount_left);
  165.  
  166.            } // end if left
  167.  
  168.         if (amount_up)
  169.            {
  170.            // move player along view vector foward
  171.  
  172.            dx=(rcos_table[cur_obj->angle] * amount_up);
  173.            dy=(rsin_table[cur_obj->angle] * amount_up);
  174.  
  175.            if (cur_extra->fly_mode) {
  176.  
  177.               // move player z based on vertical view angle
  178.  
  179.               cur_extra->base_fixed_z+=(tan_table[cur_extra->vert_angle]*amount_up);
  180.               cur_extra->cur_fixed_z=cur_extra->base_fixed_z;
  181.  
  182.               Z_Correct_Move(cur_obj);
  183.            } else {
  184.  
  185.               if (cur_obj->cur_sec->flags & VOXEL_SECTOR) {
  186.                  if (!cur_extra->fly_mode) {
  187.                     Reset_Player_Z(cur_extra);
  188.                  }
  189.               } else {
  190.  
  191.                  // Adjust player z position along a sin wave to simulate walking motion
  192.  
  193.                  cur_extra->z_move_angle=Get_Angle_Sum(cur_extra->z_move_angle, Z_ANGLE_INC);     
  194.                  cur_extra->cur_fixed_z=(cur_extra->base_fixed_z+(rsin_table[cur_extra->z_move_angle]*Z_MOVE_MAX));
  195.  
  196.               } /* endif */
  197.  
  198.            } /* endif */
  199.  
  200.            } // end if up
  201.         else
  202.         if (amount_down)
  203.            {
  204.            // move player along view vector backward
  205.  
  206.             dx=(-rcos_table[cur_obj->angle]* amount_down);
  207.             dy=(-rsin_table[cur_obj->angle] * amount_down);
  208.  
  209.             if (cur_extra->fly_mode) {
  210.  
  211.                // Move player z base on his vertical view angle
  212.  
  213.                cur_extra->base_fixed_z-=(tan_table[cur_extra->vert_angle]* amount_down);
  214.                cur_extra->cur_fixed_z=cur_extra->base_fixed_z;
  215.  
  216.                Z_Correct_Move(cur_obj);
  217.             } else {
  218.  
  219.               if (cur_obj->cur_sec->flags & VOXEL_SECTOR) {
  220.                  if (!cur_extra->fly_mode) {
  221.                     Reset_Player_Z(cur_extra);
  222.                  }
  223.               } else {
  224.  
  225.                  // Adjust player z position along a sin wave to simulate walking motion
  226.  
  227.                  cur_extra->z_move_angle=Get_Angle_Difference(cur_extra->z_move_angle, Z_ANGLE_INC);     
  228.                  cur_extra->cur_fixed_z=(cur_extra->base_fixed_z+(rsin_table[cur_extra->z_move_angle]*Z_MOVE_MAX));
  229.  
  230.               } /* endif */
  231.  
  232.             } /* endif */
  233.  
  234.            } // end if down
  235.        else
  236.           {
  237.              if (!cur_extra->fly_mode) {
  238.                 Reset_Player_Z(cur_extra);
  239.              }
  240.           }
  241.  
  242.         if (gun_shot) {
  243.            if (cur_extra->time_to_gun<=0) {
  244.            Create_Object(cur_obj->x, cur_obj->y, GUN_HEIGHT+cur_obj->z, cur_obj->angle, BULLET_TYPE, cur_obj, cur_obj->team);
  245.            cur_extra->time_to_gun=MAX_TIME_TO_GUN;
  246.            }
  247.         }
  248.  
  249.         // move player
  250.         delta_vec.x=dx;
  251.         delta_vec.y=dy;
  252.         Move_Object_Vec(cur_obj, &delta_vec);
  253.  
  254.         // check change in vertical angle
  255.         cur_extra->vert_angle=Get_Angle_Sum(cur_extra->vert_angle, amount_look_up);
  256.         cur_extra->vert_angle=Get_Angle_Difference(cur_extra->vert_angle, amount_look_down);
  257.  
  258.         switch (raw_input) {
  259.         case U_KEY:
  260.            cur_extra->base_fixed_z+=ONE;
  261.            cur_extra->cur_fixed_z+=ONE;
  262.            Z_Correct_Move(cur_obj);
  263.            break;
  264.         case D_KEY:
  265.            cur_extra->base_fixed_z-=ONE;
  266.            cur_extra->cur_fixed_z-=ONE;
  267.            Z_Correct_Move(cur_obj);
  268.            break;
  269.         case F_KEY:
  270.            cur_extra->fly_mode=(cur_extra->fly_mode == TRUE ? FALSE : TRUE);
  271.            cur_extra->z_move_angle=ANGLE_0;
  272.            cur_extra->cur_fixed_z=cur_extra->base_fixed_z;
  273.            break;
  274.         default: 
  275.            break;
  276.         }
  277.         
  278.         // test non-motion keys
  279.  
  280. if ( (cur_obj->cur_sec->tag == (HOME_SECTOR+cur_obj->team)) && 
  281.       (Scan_Inventory(cur_extra->inventory, FLAG_CLASS)!=NULL) ) {
  282.          Post_Quit(WINNING_GAME);
  283.  
  284. }
  285.  
  286. cur_obj->z=cur_extra->cur_fixed_z>>SHIFT;
  287. }
  288.  
  289. void Reset_Player_Z(pplayer_info cur_extra)
  290. {
  291.    // reset player z if they aren't moving
  292.    if ((cur_extra->z_move_angle % ANGLE_180) >= Z_ANGLE_DELTA) {
  293.  
  294.       if (cur_extra->z_move_angle % ANGLE_180 < ANGLE_90) {
  295.  
  296.          cur_extra->z_move_angle=
  297.             Get_Angle_Difference(cur_extra->z_move_angle, Z_ANGLE_INC);
  298.  
  299.       } else {
  300.  
  301.          cur_extra->z_move_angle=
  302.             Get_Angle_Sum(cur_extra->z_move_angle, Z_ANGLE_INC);
  303.  
  304.       } /* endif */
  305.  
  306.       cur_extra->cur_fixed_z=(cur_extra->base_fixed_z+
  307.          (rsin_table[cur_extra->z_move_angle]*Z_MOVE_MAX));
  308.  
  309.    } /* endif */
  310. }
  311.  
  312. void Z_Correct_Move(pobject cur_obj)
  313. {
  314.   pplayer_info cur_extra=(pplayer_info)cur_obj->extra_data;
  315.   if (cur_extra->base_fixed_z < (STEP_LENGTH<<SHIFT) ) {
  316.     cur_extra->base_fixed_z=STEP_LENGTH<<SHIFT;
  317.     cur_extra->cur_fixed_z=cur_extra->base_fixed_z;
  318.     }
  319.   long sec_height=cur_obj->cur_sec->ceil_height-cur_obj->cur_sec->floor_height;
  320.   MYFIXED max_height=(sec_height-STEP_LENGTH)<<SHIFT;
  321.   if (cur_extra->base_fixed_z > max_height) {
  322.      cur_extra->base_fixed_z=max_height;
  323.      cur_extra->cur_fixed_z=cur_extra->base_fixed_z;
  324.   }
  325. }
  326.  
  327. void Setup_Player_Inputs(pplayer_info cur_extra)
  328. {
  329. memcpy(cur_extra->tracked_inputs, cur_extra->input_buffer, MAX_TRACKED_INPUTS * sizeof(short));
  330. }
  331.  
  332. void Set_Player_Input_Buffer(PSHORT input_buffer)
  333. {
  334.   cur_input_buffer=input_buffer;
  335. }
  336.  
  337. angle_type Get_Player_Vert_Angle(pobject cur_obj)
  338. {
  339.    return ((pplayer_info)cur_obj->extra_data)->vert_angle;
  340. }
  341.  
  342. BOOL Is_Flying(pobject test_obj) {
  343.    return ((pplayer_info)test_obj->extra_data)->fly_mode;
  344. }
  345.  
  346. ULONG Player_Message(pobject send_obj, pobject receive_obj, ULONG message, pdata extra_data) {
  347.  
  348.    pplayer_info player_data=(pplayer_info)receive_obj->extra_data;
  349.  
  350.    switch (message) {
  351.    case HIT_OBJ:
  352.    case HIT_BY_OBJ:
  353.          if ((send_obj->type->obj_class==FLAG_CLASS)&&(send_obj->team!=receive_obj->team)) {
  354.             pobject_node new_node=(pobject_node)NewPtr(sizeof(object_node));
  355.             new_node->data=send_obj;
  356.             new_node->back=NULL;
  357.             new_node->front=NULL;
  358.             OL_Push_Node(new_node, player_data->inventory);
  359.             Create_Inventory_Object(send_obj, receive_obj);
  360.             Do_Shot("flag.pcx", "groovy.wav", 20, TRUE);
  361.          }
  362.          break;
  363.    default: 
  364.       break;
  365.    } /* endswitch */
  366.    return Default_Message(send_obj, receive_obj, message, extra_data);
  367. }
  368.  
  369. pobject Scan_Inventory(pobject_node inventory, ULONG search_class) {
  370.    pobject_node cur_node;
  371.    cur_node=inventory;
  372.    while (!OL_Empty_Node(cur_node)) {
  373.       if ( ((pinventory_data)cur_node->data->extra_data)->old_type->obj_class==search_class) {
  374.          return cur_node->data;
  375.       }
  376.       cur_node=OL_Next_Node(cur_node);
  377.    }
  378.    return NULL;
  379. }
  380.